package com.qiangesoft.design.factory.abstractf;

/**
 * 抽象工厂模式
 */
public class AbstractFactory {
    public static void main(String[] args) {
        // 奔驰车
        CarFactory benzFactory = new BenzFactory();
        Engine benzEngine = benzFactory.createEngine();
        Transmission benzTransmission = benzFactory.createTransmission();
        System.out.println("奔驰汽车：" + benzEngine.getName() + "+" + benzTransmission.getName());

        // 宝马车
        CarFactory bmwFactory = new BMWFactory();
        Engine bmwEngine = bmwFactory.createEngine();
        Transmission bmwTransmission = bmwFactory.createTransmission();
        System.out.println("宝马汽车：" + bmwEngine.getName() + "+" + bmwTransmission.getName());

        // 奥迪车
        CarFactory audiFactory = new AudiFactory();
        Engine audiEngine = audiFactory.createEngine();
        Transmission audiTransmission = audiFactory.createTransmission();
        System.out.println("奥迪汽车：" + audiEngine.getName() + "+" + audiTransmission.getName());
    }
}

/**
 * 1.抽象工厂接口
 */
interface CarFactory {
    Engine createEngine();

    Transmission createTransmission();
}

/**
 * 2.具体工厂类
 * 奔驰工厂、宝马工厂、奥迪工厂
 */
class BenzFactory implements CarFactory {
    @Override
    public Engine createEngine() {
        return new BenzEngine();
    }

    @Override
    public Transmission createTransmission() {
        return new BenzTransmission();
    }
}

class BMWFactory implements CarFactory {
    @Override
    public Engine createEngine() {
        return new BMWEngine();
    }

    @Override
    public Transmission createTransmission() {
        return new BmwTransmission();
    }
}

class AudiFactory implements CarFactory {
    @Override
    public Engine createEngine() {
        return new AudiEngine();
    }

    @Override
    public Transmission createTransmission() {
        return new AudiTransmission();
    }
}

/**
 * 3.产品接口1：发动机
 */
interface Engine {
    String getName();
}

/**
 * 4.产品实现类1：发动机
 */
class BenzEngine implements Engine {
    @Override
    public String getName() {
        return "奔驰发动机";
    }
}

class BMWEngine implements Engine {
    @Override
    public String getName() {
        return "宝马发动机";
    }
}

class AudiEngine implements Engine {
    @Override
    public String getName() {
        return "奥迪发动机";
    }
}

/**
 * 5.产品接口2：变速箱
 */
interface Transmission {
    String getName();
}

/**
 * 6.产品实现类2：变速箱
 */
class BenzTransmission implements Transmission {
    @Override
    public String getName() {
        return "奔驰变速箱";
    }
}

class BmwTransmission implements Transmission {
    @Override
    public String getName() {
        return "宝马变速箱";
    }
}

class AudiTransmission implements Transmission {
    @Override
    public String getName() {
        return "奥迪变速箱";
    }
}